import http from '../../utils/http.js'
import util from '../../utils/util.js';
var app = getApp();

import {
  GetStoreUserHomeApi
} from "../../utils/api.js"

import {
  GetStoreMemberListApi
} from "../../utils/newApi.js"


var listTimer = null;
// page
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navTop: 0,
    navHeight: 0,//导航高度
    windowHeight: 0,//窗口高度
    statusBarHeight: 0,//状态栏高度
    menuButtonObject: {},
    userInfo: {},
    merChantInfo: {}, // 商家信息
    storeList: [], //多商户信息
    notice: '',
    noticeId: ''
  },
//跳转外部链接
jumpAccount(e){
  return;
  wx.setStorageSync('webUrl',e.currentTarget.dataset.weburl)
  wx.navigateTo({
    url: `${e.currentTarget.dataset.url}?url=${e.currentTarget.dataset.weburl}`
  })
},
  /**
     * 生命周期函数--监听页面加载
     */
  onLoad(options) {
    if (!util.getStorageSync('gfnFlag')) {
      util.removeStorageSync('accessToken')
    }
    let that = this;
    that.GetHomeStoreList()
    // that.getNewNotice()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    that.setData({
      navTop:app.globalData.navTop,
      navHeight:app.globalData.navHeight,
      statusBarHeight:app.globalData.statusBarHeight,
      menuButtonObject:app.globalData.menuButtonObject,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return{
      title:'下楼休闲吧商户端'
    }
  },

  onPullDownRefresh() {
    wx.showNavigationBarLoading()
    wx.showLoading()
    this.GetHomeStoreList()
    setTimeout(function() {
      // 取消标题栏进度条效果
      wx.hideNavigationBarLoading()
      // 取消页面刷新动画
      wx.stopPullDownRefresh()
      wx.hideLoading()
    },1500)
  },

  requestMessage() {
    wx.requestSubscribeMessage({
      tmplIds: ['KAytSd61RbmfPvZ6s-mBtkLwriR0uzPUzCFlioCMyzg', '3xPqcPo2_yMAuRekmENnh58HdHcT0lefR-AwBndy-qI'],
      success(res) {
        wx.getSetting({
          withSubscriptions:true,
          success(suc) {
          }
        })
      },
      complete (res) {
        switch(res['KAytSd61RbmfPvZ6s-mBtkLwriR0uzPUzCFlioCMyzg', '3xPqcPo2_yMAuRekmENnh58HdHcT0lefR-AwBndy-qI']){
          case "accept":
            wx.getSetting({
              withSubscriptions:true,
              success(suc) {
              }
            })
            break;
          case "reject":
              // 拒绝授权订阅消息
              wx.showModal({
                content: '拒绝授权后将收不到用户预定通知哦,请再次授权',
                title: '提示',
                success(res){
                  console.log('再次授权')
                  if(res.confirm){
                   console.log('再次授权成功')
                  }else{
                     console.log('再次授权失败')

                    resolve();
                  }
                }
              })
            break;
        }
      },
    })
  },

  WeixinServer() {
    wx.openCustomerServiceChat({
      extInfo: {
        url: 'https://work.weixin.qq.com/kfid/kfc11f5184d7762c658',
      },
      corpId: 'ww106853c8ad050781',
      success(res) {
        this.requestMessage()
      },
      fail(fail) {
        console.log(fail)
      }
    })
  },

  GetHomeStoreList(){
    console.log("执行")
    let that = this
    GetStoreMemberListApi().then(res=>{
      console.log(res,"res")
      if (res.code === 0) {
          // res.data.profitCount = Math.floor(res.data.profitCount *100)/100;
          that.setData({
            merChantInfo : res.data,
            storeList : res.data.List
          })
      }
    }).catch(err=>{
      console.log(err,"err")
    })
    // http.get('/api/store/v1/user/home').then(res => {
    //   if (res.code === 200) {
    //     res.data.profitCount = Math.floor(res.data.profitCount *100)/100;
    //     that.setData({
    //       merChantInfo : res.data,
    //       storeList : res.data.list
    //     })
    //   } else {
    //     wx.redirectTo({
    //       url: '/pages/login/login'
    //     })
    //   }
    // }).catch(() => {
    //   wx.redirectTo({
    //     url: '/pages/login/login'
    //   })
    // })
  },

  // 跳转单个商家
  JumpStoreDetail(e){
    let that = this
    wx.navigateTo({
      url: '/pages/home/home?id=' + e.currentTarget.dataset.store + '&admin=' + e.currentTarget.dataset.admin,
    })
  },

  // 查询最新的公告
  getNewNotice() {
    http.post('/api/store/v1/platform/notice').then(res => {
      if (res.code === 200 && res.data.id) {
        this.setData({
          notice: res.data.title,
          noticeId: res.data.id
        })
      }
    })
  },
  gotoNoticeList(e) {
    let that = this
    wx.navigateTo({
      url: '/pages/notice/noticeDetail/noticeDetail?id=' + this.data.noticeId
    })
  },
  // WeixinServer(){
  //   wx.openCustomerServiceChat({
  //     extInfo: {
  //       url:'https://work.weixin.qq.com/kf/' + "kfc11f5184d7762c658"
  //     },
  //     corpId: 'ww106853c8ad050781',
  //   })
  // }
})